// proxySubAgDetail.js
const app = getApp()
var util = require("../../utils/util.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    update:true,
    rateUpdate: true,
    disabledStyle:'color:#b2b2b2;--field-input-text-color:#b2b2b2',
    detail:{},
    original:{},
    surplusRate: '',

  },
  // 表单数值改变
  onChange: function(detail, event) {// detail 为当前输入的值
    var that = this;
    that.setData({
      [detail]: event.detail
    })
  },
  nameChange:function(event){
    this.onChange('detail.name', event)
  },
  telChange: function (event) {
    this.onChange('detail.telphone', event)
  },
  addChange: function (event) {
    this.onChange('detail.area', event)
  },
  rateChange: function (event) {
    this.onChange('detail.commission', event)
  },
  // 更改表单可修改状态
  toUpdate:function(){
    var that = this;
    var detail = that.data.detail
    if (that.data.update == false){
      detail = JSON.parse(that.data.original);
    }
    that.setData({
      detail:detail,
      update: !that.data.update,
      rateUpdate: that.data.isAdmin?true:false
    })
  },
  rateLimit: function (event) {
    var surplusRate = this.data.surplusRate
    if (event.detail.value > surplusRate) {
      this.setData({
        ['detail.commission']: surplusRate
      })
    }
    if (event.detail.value < 0) {
      this.setData({
        ['detail.commission']: 0
      })
    }
  },
  // 保存
  save: function () {
    var that = this;
    var surplusRate = this.data.surplusRate
    if (that.data.rate > surplusRate) {
      that.setData({
        ['detail.commission']: surplusRate
      })
    }
    var detail = that.data.detail;
    var data = {
      id: detail.id,
      name: detail.name,
      tel: detail.telphone,
      address: detail.area,
      commission: detail.commission
    };
    wx.showLoading({
      title: '加载中...',
    })
    app.post('agent/submitAgentInfo', data).then((res) => {
      wx.hideLoading();
      if (res.code == 200) {
        wx.showToast({
          title: "修改成功",
          icon: 'success',
          duration: 2000
        })
        that.setData({
          update: true
        })
        var pages = getCurrentPages();
        var beforePage = pages[pages.length - 2];
        beforePage.onPullDownRefresh();
      } else {
        wx.showToast({
          title: "提交失败",
          icon: 'loading',
          duration: 2000
        })
      }
    }).catch((errMsg) => {
      console.log(errMsg);
      wx.hideLoading();
      wx.showToast({
        title: errMsg,
        icon: 'loading',
        duration: 2000
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var detail = JSON.parse(options.detail);
    // var rateUpdate =false;
    var isAdmin = false;
    if(options.level){
      // rateUpdate = options.level?true:false,
      isAdmin = true
    }
    this.setData({
      original: options.detail,
      detail: detail,
      surplusRate: app.globalData.agentRate,
      // rateUpdate: rateUpdate,
      isAdmin: isAdmin
    })
  }
})